package com.practice.nacos.discovery.consumer.example.controller;

import com.practice.nacos.discovery.consumer.example.service.EchoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author yangdejun
 * @date 2020/12/02
 **/
@RestController
public class TestController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private EchoService echoService;

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping(value = "/echo-rest/{str}")
    public String rest(@PathVariable String str) {
        String forObject = restTemplate.getForObject("http://service-provider/echo/" + str, String.class);
        return forObject;
    }

    @GetMapping(value = "/echo-feign/{str}")
    public String feign(@PathVariable String str) {
        return echoService.echo(str);
    }

    @GetMapping(value = "/services")
    public Object services() {
        return discoveryClient.getServices();
    }

    @GetMapping(value = "/services/{service}")
    public Object client(@PathVariable String service) {
        return discoveryClient.getInstances(service);
    }
}
